import {LoginScene} from "db://assets/scripts/LoginScene";
import {Long} from "protobufjs";
import ProtoEnumMsg from "db://assets/scripts/proto/ProtoEnumMsg.js";
import AccountMsg from "db://assets/scripts/proto/AccountMsg.js";
import {RoleInfo} from "db://assets/scripts/obj/RoleInfo";
import {FloatingTextManager} from "db://assets/scripts/common/FloatingTextManager";
import {ProtoDispatcher} from "db://assets/scripts/common/ProtoDispatcher";
import {Pack, PackCodec} from "db://assets/scripts/common/Pack";
import {NetManager} from "db://assets/scripts/common/NetManager";
import ISimpleRole = AccountMsg.ISimpleRole;
import Singleton from "db://assets/scripts/common/Singleton";

export class AccountModule extends Singleton {

    private loginScene: LoginScene = null;

    private accountName: string;
    private password: string;

    private accountId: Long;

    private roleList: RoleInfo[];

    init(loginScene: LoginScene) {
        this.loginScene = loginScene;

        ProtoDispatcher.registerDecoder(ProtoEnumMsg.CMD.ID.ACCOUNT_LOGIN.valueOf(), data => AccountMsg.S2CAccountLogin.decode(data));
        ProtoDispatcher.registerHandler(ProtoEnumMsg.CMD.ID.ACCOUNT_LOGIN.valueOf(), this.accountLoginResp.bind(this));

        ProtoDispatcher.registerDecoder(ProtoEnumMsg.CMD.ID.ACCOUNT_REGISTER.valueOf(), data => AccountMsg.S2CAccountRegister.decode(data));
        ProtoDispatcher.registerHandler(ProtoEnumMsg.CMD.ID.ACCOUNT_REGISTER.valueOf(), this.accountRegisterResp.bind(this));

        ProtoDispatcher.registerDecoder(ProtoEnumMsg.CMD.ID.ROLE_CREATE.valueOf(), data => AccountMsg.S2CRoleCreate.decode(data));
        ProtoDispatcher.registerHandler(ProtoEnumMsg.CMD.ID.ROLE_CREATE.valueOf(), this.roleCreateResp.bind(this));

        ProtoDispatcher.registerDecoder(ProtoEnumMsg.CMD.ID.ROLE_LOGIN.valueOf(), data => AccountMsg.S2CRoleLogin.decode(data));
        ProtoDispatcher.registerHandler(ProtoEnumMsg.CMD.ID.ROLE_LOGIN.valueOf(), this.roleCreateResp.bind(this));
    }

    /**
     * 发送登录协议
     * @param accountName
     * @param password
     */
    accountLogin(accountName: string, password: string) {
        let c2SAccountLogin = AccountMsg.C2SAccountLogin.create({accountName: accountName, password: password});
        let byte_array = AccountMsg.C2SAccountLogin.encode(c2SAccountLogin).finish();
        let cmdId = ProtoEnumMsg.CMD.ID.ACCOUNT_LOGIN.valueOf();
        let data = new Pack(cmdId, byte_array);
        this.accountName = accountName;
        this.password = password;
        NetManager.getInstance().sendMsg(PackCodec.encode(data));
    }

    /**
     * 账号登录消息返回
     */
    accountLoginResp(data: AccountMsg.S2CAccountLogin) {
        if (!data.success) {
            console.log("account login failed. ")
            FloatingTextManager.showText("账号登陆失败，请检查账号密码");
            return;
        }
        this.accountId = <Long>data.accountId;
        this.roleList = data.roles.map(role => this.makeRoleInfo(role));
        this.loginScene.onAccountLoginSuccess(this.roleList);
    }

    /**
     * 用协议里的SimpleRole组装一个RoleInfo
     * @param role : ISimpleRole
     * @private
     */
    private makeRoleInfo(role: ISimpleRole) {
        let roleInfo = new RoleInfo();
        roleInfo.roleId = <Long>role.roleId;
        roleInfo.name = role.name;
        roleInfo.level = role.level;
        return roleInfo;
    }

    /**
     * 账号注册
     */
    accountRegister(accountName: string, password: string) {
        let c2SAccountRegister = AccountMsg.C2SAccountRegister.create({accountName: accountName, password: password});
        let byte_array = AccountMsg.C2SAccountRegister.encode(c2SAccountRegister).finish();
        let cmdId = ProtoEnumMsg.CMD.ID.ACCOUNT_REGISTER.valueOf();
        let data = new Pack(cmdId, byte_array);
        this.accountName = accountName;
        this.password = password;

        NetManager.getInstance().sendMsg(PackCodec.encode(data));
    }

    accountRegisterResp(data: AccountMsg.S2CAccountRegister) {
        console.log("accountRegisterResp", data);
        if (data.success) { // 注册成功自动登录
            FloatingTextManager.showText("账号注册成功");
            this.accountLogin(this.accountName, this.password);
        }
    }

    /**
     * 创建角色
     */
    roleCreate(roleName: string) {
        let c2SRoleCreate = AccountMsg.C2SRoleCreate.create();
        c2SRoleCreate.name = roleName;
        let byte_array = AccountMsg.C2SRoleCreate.encode(c2SRoleCreate).finish();
        let cmdId = ProtoEnumMsg.CMD.ID.ROLE_CREATE.valueOf();
        let data = new Pack(cmdId, byte_array)

        NetManager.getInstance().sendMsg(PackCodec.encode(data));
    }

    roleCreateResp(data: AccountMsg.S2CRoleCreate) {
        this.roleList.push(this.makeRoleInfo(data.simpleRole));
        this.loginScene.onAccountLoginSuccess(this.roleList);

    }

}